﻿/*
 * My Timer - cdTimerControl.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Windows.Forms;

namespace My_Timer
{
    public class TimerControl
    {
        public static void StartTimer(int i_Tag)
        {
            if (GlobalClass.r_TimerControl.ContainsKey(i_Tag) == false)
            {
                Timer t_TimerControl = new Timer();
                t_TimerControl.Tick += new EventHandler(timerControl_Tick);

                GlobalClass.r_TimerControl.Add(i_Tag, t_TimerControl);
            }

            TimersArray t_TimersArray;
            t_TimersArray = GlobalClass.r_Timers[i_Tag];

            string s = "";

            s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.tDays,
                t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

            if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
            {
                t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());

                int i_IndexOfKey = t_TimersArray.iIndexOfKey;

                GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Text = t_TimersArray.Name + " (Running)";
                GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].SubItems[1].Text = s;
            }

            if (t_TimersArray.IsFormEnable == true)
            {
                t_TimersArray.TimerForm.pictureBox1.Image = global::My_Timer.Properties.Resources.media_playback_pause_16x16;

                s = Formatting.Formatting.formatDaysHoursMinutesSecondsShort(t_TimersArray.tDays,
                    t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

                t_TimersArray.TimerForm.labelTimer.Text = s;
            }

            t_TimersArray.timerState = 1;

            GlobalClass.r_Timers[i_Tag] = t_TimersArray;

            GlobalClass.r_TimerControl[i_Tag].Tag = i_Tag;
            GlobalClass.r_TimerControl[i_Tag].Interval = 1000;
            GlobalClass.r_TimerControl[i_Tag].Enabled = true;
        }

        public static void PauseTimer(int i_Tag)
        {
            if (GlobalClass.r_TimerControl.ContainsKey(i_Tag) == true)
            {
                TimersArray t_TimersArray;
                t_TimersArray = GlobalClass.r_Timers[i_Tag];

                t_TimersArray.timerState = 2;

                GlobalClass.r_TimerControl[i_Tag].Enabled = false;

                string s = "";

                s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.tDays,
                    t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

                if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
                {
                    t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());

                    int i_IndexOfKey = t_TimersArray.iIndexOfKey;

                    GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Text = t_TimersArray.Name + " (Paused)";
                    GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].SubItems[1].Text = s;
                }

                if (t_TimersArray.IsFormEnable == true)
                {
                    t_TimersArray.TimerForm.pictureBox1.Image = global::My_Timer.Properties.Resources.media_playback_start_16x16;

                    s = Formatting.Formatting.formatDaysHoursMinutesSecondsShort(t_TimersArray.tDays,
                    t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

                    t_TimersArray.TimerForm.labelTimer.Text = s;
                }

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;
            }
        }

        public static void StopTimer(int i_Tag)
        {
            if (GlobalClass.r_TimerControl.ContainsKey(i_Tag) == true)
            {
                GlobalClass.r_TimerControl[i_Tag].Enabled = false;
            }

            TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

            t_TimersArray.tDays = t_TimersArray.Days;
            t_TimersArray.tHours = t_TimersArray.Hours;
            t_TimersArray.tMinutes = t_TimersArray.Minutes;
            t_TimersArray.tSeconds = t_TimersArray.Seconds;
            t_TimersArray.timerState = 0;

            string s = "";

            s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.tDays,
                t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

            if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
            {
                t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());

                int i_IndexOfKey = t_TimersArray.iIndexOfKey;

                GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Text = t_TimersArray.Name;
                GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].SubItems[1].Text = s;
            }

            if (t_TimersArray.IsFormEnable == true)
            {
                t_TimersArray.TimerForm.pictureBox1.Image = global::My_Timer.Properties.Resources.media_playback_start_16x16;

                s = Formatting.Formatting.formatDaysHoursMinutesSecondsShort(t_TimersArray.tDays,
                    t_TimersArray.tHours, t_TimersArray.tMinutes, t_TimersArray.tSeconds);

                t_TimersArray.TimerForm.labelTimer.Text = s;
            }

            GlobalClass.r_Timers[i_Tag] = t_TimersArray;
        }

        private static void timerControl_Tick(object sender, EventArgs e)
        {
            //ok, i messed up here, this is quick dirty hack..
            //I'm not sure if the sender can be other than Timer type

            Timer hihi = (Timer)sender;

            int i_Tag = (int)hihi.Tag;

            int vDays, vHours, vMinutes, vSeconds;

            if (GlobalClass.r_Timers.ContainsKey(i_Tag) == true)
            {

                TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

                if ((t_TimersArray.tHours == 0) && (t_TimersArray.tDays > 0))
                {
                    t_TimersArray.tDays--;
                    t_TimersArray.tHours = 24;
                }

                if ((t_TimersArray.tMinutes == 0) && (t_TimersArray.tHours > 0))
                {
                    t_TimersArray.tHours--;
                    t_TimersArray.tMinutes = 60;
                }

                if ((t_TimersArray.tSeconds == 0) && (t_TimersArray.tMinutes > 0))
                {
                    t_TimersArray.tMinutes--;
                    t_TimersArray.tSeconds = 60;
                }

                t_TimersArray.tSeconds--;

                if (t_TimersArray.tSeconds < 0)
                {
                    t_TimersArray.tSeconds = 0;
                }

                vDays = t_TimersArray.tDays;
                vHours = t_TimersArray.tHours;
                vMinutes = t_TimersArray.tMinutes;
                vSeconds = t_TimersArray.tSeconds;

                while (vSeconds >= 60)
                {
                    vSeconds = vSeconds - 60;
                    vMinutes++;
                }

                while (vMinutes >= 60)
                {
                    vMinutes = vMinutes - 60;
                    vHours++;
                }

                while (vHours >= 24)
                {
                    vHours = vHours - 24;
                    vDays++;
                }

                if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
                {
                    t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());

                    int i_IndexOfKey = t_TimersArray.iIndexOfKey;
                    string s = "";

                    s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(vDays, vHours, vMinutes,
                        vSeconds);

                    GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].SubItems[1].Text = s;
                }

                if (t_TimersArray.IsFormEnable == true)
                {
                    string s = "";

                    s = Formatting.Formatting.formatDaysHoursMinutesSecondsShort(vDays, vHours, vMinutes, vSeconds);

                    t_TimersArray.TimerForm.labelTimer.Text = s;
                }

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;

                // Once we hit 0..
                if ((t_TimersArray.tDays + t_TimersArray.tHours + t_TimersArray.tMinutes + t_TimersArray.tSeconds) == 0)
                {
                    t_TimersArray.tDays = t_TimersArray.Days;
                    t_TimersArray.tHours = t_TimersArray.Hours;
                    t_TimersArray.tMinutes = t_TimersArray.Minutes;
                    t_TimersArray.tSeconds = t_TimersArray.Seconds;

                    // Restart timer or stop?
                    switch (t_TimersArray.OnTimerEnd)
                    {
                        case 0:
                            GlobalClass.r_TimerControl[i_Tag].Enabled = false;

                            if (t_TimersArray.IsFormEnable == true)
                            {
                                t_TimersArray.TimerForm.pictureBox1.Image =
                                    global::My_Timer.Properties.Resources.media_playback_start_16x16;
                                t_TimersArray.TimerForm.labelTimer.Text = "00:00:00:00";
                            }

                            if ((GlobalClass.r_frmMain.Created == true) || (GlobalClass.r_frmMain.Visible == true))
                            {
                                t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(i_Tag.ToString());
                                int i_IndexOfKey = t_TimersArray.iIndexOfKey;

                                GlobalClass.r_frmMain.lvTimers.Items[i_IndexOfKey].Text = t_TimersArray.Name + " (Finished)";
                            }

                            break;

                        case 1:

                            break;
                    }

                    GlobalClass.r_Timers[i_Tag] = t_TimersArray;

                    // Do we need to play the audio?
                    if (t_TimersArray.PlayAudio == true)
                    {
                        switch (File.Exists(t_TimersArray.PlayAudioPath))
                        {
                            case true:
                                frmTray.doPlayAudio(0, t_TimersArray.PlayAudioPath);

                                break;

                            case false:
                                string a = "";
                                a = t_TimersArray.PlayAudioPath;
                                if (a.Length == 0)
                                {
                                    a = "none";
                                }
                                MessageBox.Show("Unable to play \"" + a + "\", file does not exist.",
                                    "My Timer", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                break;
                        }
                    }

                    // Show messagebox if enabled
                    if (t_TimersArray.PopupMsgBox == true)
                    {
                        if (GlobalClass.drMessageBoxShowed.ContainsKey(i_Tag) == false)
                        {
                            GlobalClass.drMessageBoxShowed.Add(i_Tag, DialogResult.OK);
                        }

                        switch (GlobalClass.drMessageBoxShowed[i_Tag])
                        {
                            case DialogResult.OK:
                                frmDummyMessageBox r_frmDummyMessageBox = new frmDummyMessageBox();
                                GlobalClass.drMessageBoxShowed[i_Tag] = DialogResult.None;
                                r_frmDummyMessageBox.TopMost = true;
                                r_frmDummyMessageBox.Activate();
                                GlobalClass.drMessageBoxShowed[i_Tag] = MessageBox.Show(r_frmDummyMessageBox, t_TimersArray.MsgBoxString,
                                    "My Timer", MessageBoxButtons.OK);

                                r_frmDummyMessageBox.Close();
                                r_frmDummyMessageBox.Dispose();
                                break;

                            case DialogResult.None:
                                // simply skip if no DialogResult.OK in dr_MessageBoxShowed

                                break;
                        }
                    }

                    // Run application
                    if (t_TimersArray.openApp == true)
                    {
                        cThreading.try_Start(t_TimersArray.appLocation);
                    }

                    // Shutdown/restart?
                    if (t_TimersArray.ShutdownComputer > 0)
                    {
                        if (GlobalClass.r_frmShutDown == null)
                        {
                            GlobalClass.r_frmShutDown = new frmShutDown();
                        }
                        if ((GlobalClass.r_frmShutDown.IsDisposed == true) || (GlobalClass.r_frmShutDown.Created == false))
                        {
                            GlobalClass.r_frmShutDown = new frmShutDown();
                        }

                        GlobalClass.r_frmShutDown.Tag = t_TimersArray.ShutdownComputer;
                        GlobalClass.r_frmShutDown.Show();
                        GlobalClass.r_frmShutDown.Activate();
                    }
                }
            }
        }
    }
}